/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-10 16:49:18
 * @LastEditTime: 2022-12-27 18:27:58
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import { SET_TABLE_DATA, SET_CHART_DATA } from '../constants';
import { transferForTable, transferForStat } from '@/api/statistics/transferReceive';
import { TransferReducer } from '../reducers/TransferReducer';
const initialState = {
  dataList: [],
  chartList: []
};
const TransferContext = createContext();
const TransferContextProvider = (props) => {
  const [state, dispatch] = useReducer(TransferReducer, initialState);
  const actions = {
    setChartData(data) {
      transferForStat({ ...data }).then((res) => {
        const { data } = res;
        dispatch({ type: SET_CHART_DATA, data });
      });
    },
    setTableData(data) {
      transferForTable({ ...data }).then((res) => {
        const { data } = res;
        dispatch({ type: SET_TABLE_DATA, data });
      });
    }
  };
  useEffect(() => {}, []);
  return (
    <TransferContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </TransferContext.Provider>
  );
};
export const useTransferContext = () => {
  return useContext(TransferContext);
};
export { TransferContext, TransferContextProvider };
